/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.collablet.coop.recommend;

import java.util.List;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.com.caelum.vraptor.view.Results;
import br.org.groupwareworkbench.aun.news.NewsMgrInstance;
import br.org.groupwareworkbench.core.framework.CollabletInstance;
import br.org.groupwareworkbench.core.bd.GenericEntity;

@RequestScoped
@Resource
public class RecommendMgrController {

    private final Result result;
    
    public RecommendMgrController(Result result) {
        this.result = result;
    }

    @Post
    @Path(value = "/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}")
    public void save(final CollabletInstance collablet, final RecommendMgrInstance recommendMgr, Recommend recommend) {
        recommendMgr.saveRecommend(recommend);
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(collablet, recommendMgr);
    }

    @Get
    @Path(value = "/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/configure")
    public void listConfigure(final CollabletInstance collablet, RecommendMgrInstance recommendMgr) {
        result.include("numRelated", String.valueOf(recommendMgr.getNumRelated()));
        result.include("collabletInstance", collablet);
        result.include("recommendMgr", recommendMgr);
    }

    @Post
    @Path(value = "/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/updateNumRelated")
    public void setNumRelated(final CollabletInstance collablet, RecommendMgrInstance recommendMgr, final String max) {
        recommendMgr.setNumRelated(Integer.parseInt(max));
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(collablet, recommendMgr);
    }
    
    @Get
    @Path(value = "/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/calcRecommned")
    public void recalRecommend(final CollabletInstance collablet, RecommendMgrInstance recommendMgr) {
        recommendMgr.deleteAll();
        recommendMgr.calculeAll(((NewsMgrInstance) collablet).list());
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(collablet, recommendMgr);
    }

    @Post
    @Path(value="/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/getRelated/{id}")
    public List<GenericEntity> getRelated(RecommendMgrInstance recommendMgr, GenericEntity genericEntity) {
        recommendMgr.getRelated(genericEntity);
        return null;
    }

    @Post
    @Path(value="/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/searchRelated/{id}")
    public void generateRecommend(RecommendMgrInstance recommendMgr, CollabletInstance collablet, List<GenericEntity> genericsEntity, GenericEntity genericEntity) {
        recommendMgr.generateRelated(collablet, genericsEntity, genericEntity);
    }
    
    @Get
    @Path(value = "/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/configureMethod")
    public void configureMethod(final CollabletInstance collablet, RecommendMgrInstance recommendMgr) {
        result.include("classes", recommendMgr.listClassCanBeRecommend(collablet.getClass().getPackage().getName()));
        result.include("collabletInstance", collablet);
        result.include("recommendMgr", recommendMgr);
    }
    
    @Post
    @Path(value="/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/updateMethodsEnable")
    public void updateMethodsEnable(final CollabletInstance collablet,final RecommendMgrInstance recommendMgr, List<String> enabledMethodList) throws SecurityException, NoSuchMethodException {

        recommendMgr.setEnabledMethodCanRecomend(collablet.getClass().getPackage().getName(), enabledMethodList);        

        result.include("recommendMgr", recommendMgr);
        result.include("collabletInstance", collablet);
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(collablet, recommendMgr);
    }
}
